import fs from "fs";
import path from "path";
// 判断是否为文件夹还是文件
function isDirectory(dir = [], basePath = "../src") {
  let resolveResult = {
    dir: [],
    file: [],
  };
  dir.forEach((ele) => {
    const stats = fs.statSync(path.join(__dirname, basePath, ele));
    if (stats.isDirectory()) {
      resolveResult.dir.push(ele);
    } else {
      resolveResult.file.push(ele);
    }
  });
  return resolveResult;
}

// 拿到所有src下的文件
function getTotalSrcDir(keyName) {
  let obj = {};
  const dir = fs.readdirSync(path.resolve(__dirname, "../src"));
  const zks = isDirectory(dir);
  zks.dir.forEach((name) => {
    obj[`${keyName}${name}`] = path.resolve(__dirname, "../src" + "/" + name);
    obj[`${keyName}`] = path.resolve(__dirname, "../src");
  });
  // console.log(obj);
  return obj;
}
export default ({ keyName = "@" } = {}) => {
  return {
    config: (config, { mode, command }) => {
      const obj = getTotalSrcDir(keyName);
      return {
        resolve: {
          alias: obj,
        },
      };
    },
  };
};
